package com.csoa.community.activity;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.csoa.community.R;
import com.csoa.community.datepicker.OnWheelChangedListener;
import com.csoa.community.datepicker.WheelView;
import com.csoa.community.datepicker.adapter.NumericWheelAdapter;
import com.csoa.community.utils.StringUtils;

public class OffDocSearchActivity extends BaseActivity {

	private Button btnSearch;
	private EditText etTitle;
	private EditText etOwner;
	private EditText etFrom;
	private TextView tvBeginTime;
	private TextView tvEndTime;
	private int START_YEAR;
	private int END_YEAR;
	private View mViewDatePicker;
	private Animation mAnimationPushUpIn;
	private Animation mAnimationPushUpOut;

	@Override
	public void initUI() {
		setContentView(R.layout.ui_offdoc_search);

		mViewDatePicker = this.findViewById(R.id.layout_datepicker);

		mAnimationPushUpIn = AnimationUtils.loadAnimation(this,
				R.anim.slide_up_in);
		mAnimationPushUpOut = AnimationUtils.loadAnimation(this,
				R.anim.slide_down_out);

		etTitle = (EditText) this.findViewById(R.id.searchTitleET);
		etOwner = (EditText) this.findViewById(R.id.searchOwnerET);
		etFrom = (EditText) this.findViewById(R.id.searchFromET);

		tvBeginTime = (TextView) this.findViewById(R.id.searchBeginTimeTV);
		tvEndTime = (TextView) this.findViewById(R.id.searchEndTimeTV);
		btnSearch = (Button) this.findViewById(R.id.offDocSearchBtnSearch);
		btnSearch.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				String title = etTitle.getText().toString();
				String owner = etOwner.getText().toString();
				String from = etFrom.getText().toString();
				String beginTime = tvBeginTime.getText().toString();
				String endTime = tvEndTime.getText().toString();

				if (StringUtils.isEmpty(title) && StringUtils.isEmpty(owner)
						&& StringUtils.isEmpty(from)
						&& StringUtils.isEmpty(beginTime)
						&& StringUtils.isEmpty(endTime)) {
					OffDocSearchActivity.this.showToast("请填写任一搜索条件");
					return;

				}
				
				if(!StringUtils.isEmpty(beginTime) && !StringUtils.isEmpty(endTime)) {
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
					
					try {
						long timeStart=sdf.parse(beginTime).getTime();
						long timeEnd =sdf.parse(endTime).getTime();
						if(timeStart > timeEnd) {
							OffDocSearchActivity.this.showToast("开始时间不能大于结束时间");
							return;
						}
					} catch (ParseException e) {
						e.printStackTrace();
					}
					
				}
				
				
				
				
				Intent intent = new Intent();
				intent.putExtra("title", title);
				intent.putExtra("owner", owner);
				intent.putExtra("from", from);
				intent.putExtra("beginTime", beginTime);
				intent.putExtra("endTime", endTime);
				OffDocSearchActivity.this.setResult(2, intent);
				finish();

			}
		});
		tvBeginTime.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDateTimePicker(tvBeginTime);
			}
		});
		tvEndTime.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDateTimePicker(tvEndTime);
			}
		});
	}

	@Override
	public void initData() {

	}

	void updateDays(int startYear, WheelView year, WheelView month,
			WheelView day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, startYear + year.getCurrentItem());
		calendar.set(Calendar.MONTH, month.getCurrentItem());

		int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		day.setViewAdapter(new NumericWheelAdapter(this, 1, maxDays, null, "日"));
		// int curDay = Math.min(maxDays, day.getCurrentItem() + 1);
		// day.setCurrentItem(curDay - 1, true);
	}

	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	private void showDateTimePicker(final TextView tv) {
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date dateInput = new Date(System.currentTimeMillis());
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateInput);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DATE);
//		int hour = calendar.get(Calendar.HOUR_OF_DAY);
//		int minute = calendar.get(Calendar.MINUTE);

		View view = findViewById(R.id.layout_datepicker);

		// 年
		final WheelView wv_year = (WheelView) view.findViewById(R.id.year);
		START_YEAR = year - 3;
		END_YEAR = year + 8;
		wv_year.setViewAdapter(new NumericWheelAdapter(this, START_YEAR,
				END_YEAR, null, "年"));// 设置"年"的显示数据
		wv_year.setCurrentItem(year - START_YEAR);// 初始化时显示的数据

		// 月
		final WheelView wv_month = (WheelView) view.findViewById(R.id.month);
		wv_month.setViewAdapter(new NumericWheelAdapter(this, 1, 12, null, "月"));
		wv_month.setCyclic(true);
		wv_month.setCurrentItem(month);

		// 日
		final WheelView wv_day = (WheelView) view.findViewById(R.id.day);
		updateDays(START_YEAR, wv_year, wv_month, wv_day);
		wv_day.setCyclic(true);
		wv_day.setCurrentItem(day - 1);

//		// 时
//		final WheelView wv_hours = (WheelView) view.findViewById(R.id.hour);
//		wv_hours.setViewAdapter(new NumericWheelAdapter(this, 0, 23, null, "时"));
//		wv_hours.setCyclic(true);
//		wv_hours.setCurrentItem(hour);
//
//		// 分
//		final WheelView wv_mins = (WheelView) view.findViewById(R.id.mins);
//		wv_mins.setViewAdapter(new NumericWheelAdapter(this, 0, 59, "%02d", "分"));
//		wv_mins.setCyclic(true);
//		wv_mins.setCurrentItem(minute);

		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(START_YEAR, wv_year, wv_month, wv_day);
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				updateDays(START_YEAR, wv_year, wv_month, wv_day);
			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);

		Button btn_sure = (Button) view.findViewById(R.id.btn_datetime_sure);
		Button btn_cancel = (Button) view
				.findViewById(R.id.btn_datetime_cancel);
		// 确定
		btn_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// 如果是个数,则显示为"02"的样式
				String parten = "00";
				DecimalFormat decimal = new DecimalFormat(parten);

				StringBuilder time = new StringBuilder();
				time.append(wv_year.getCurrentItem() + START_YEAR);
				time.append("-");
				time.append(decimal.format(wv_month.getCurrentItem() + 1));
				time.append("-");
				time.append(decimal.format(wv_day.getCurrentItem() + 1));
				time.append(" ");
				tv.setText(time.toString());
				showDatePicker();
			}
		});
		// 取消
		btn_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				showDatePicker();
			}
		});
		showDatePicker();

	}

	private void showDatePicker() {
		if (mViewDatePicker.getVisibility() == View.GONE) {
			mViewDatePicker.setVisibility(View.VISIBLE);
			mViewDatePicker.startAnimation(mAnimationPushUpIn);
		} else {
			mViewDatePicker.setVisibility(View.GONE);
			mViewDatePicker.startAnimation(mAnimationPushUpOut);
		}
	}
}
